﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using cocos2d;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CocosDenshion;


namespace CampaignWP7
{
    public class GameLayerLayout
    {
        public static CCRect leftTouchRect;
        public static CCRect rightTouchRect;
        public static int boundaryPolo = 125;
        ////////////////////////////////////////
        public static int MaxFloor = 4;
        public static int FloorHigh = 720 / 6;
        public static CCPoint[] FloorPoint;
        //-------------------------------------先放弃使用楼层什么的了  
        public static int PlayerPole = 75;  //X lock
        public static int PlayerMaxHigh = 700;
        public static int PlayerMinHigh = 130;

        //------------------------------------
        public static CCPoint WolfBornPoint;
        public static int WolfWalkToJumpMaxLengh = 300;
        public static int WolfWalkToJumpMinLengh = 100;
        public static int GoundHigh = 90;

        //

        public static void Init()
        {
            //this size should be  480x800 因为是竖屏幕
            CCSize size =  CCDirector.sharedDirector().getWinSize();
            leftTouchRect = new CCRect(0, 0, boundaryPolo, size.height);
            rightTouchRect = new CCRect(boundaryPolo, 0, size.width - boundaryPolo, size.height);
            FloorPoint = new CCPoint[MaxFloor+1];
            for (int i = 0; i <= MaxFloor;++i )
            {
                FloorPoint[i] = new CCPoint(65, FloorHigh * i + 130);
            }
            //
            WolfBornPoint = new CCPoint(size.width - 20, size.height - 64);

        }

    }
    public class GameLayer : CCLayer
    {
        //     
        public BunnyPlayer MainPlayer;
        public WolfEmitter WolfHouse;
        public BossEmitter BossHouse;
        private SEL_SCHEDULE updateLayer;
        private CCLabelBMFont pGameLabel;
        public BackGround BG;
        //
        public CCSprite WeatherSprite;
        public ResDef.WeatherType CurrentWeather;
        //
        public static bool m_bBossAppear = false;

        //------------------------------------
        public static CCPoint PlayerPosition; //有些npc需要知道主角的位置，就在这里找吧，gamelayer每帧记录player的位置

        public override bool init()
        {

            if (!base.init())
            {
                return false;          
            }
            //
            CCDirector.sharedDirector().deviceOrientation = ccDeviceOrientation.CCDeviceOrientationPortrait;

            //////////////////////////////
            // 1. super init first
            if (!base.init())
            {
                return false;
            }

            this.m_bIsTouchEnabled = true;

            /////////////////////////////
            // 3. add your codes below...

            // add a label shows "Hello World"
            // create and initialize a label
            pGameLabel = CCLabelBMFont.labelWithString("0", @"fonts/futura-48");
            pGameLabel.anchorPoint = new CCPoint(0.5f, 0.5f);
            // ask director the window size
            CCSize size = CCDirector.sharedDirector().getWinSize();
            // position the label on the center of the screen
            pGameLabel.position = new CCPoint(size.width / 2, size.height - 32);
            // add the label as a child to this layer
            this.addChild(pGameLabel, 100);
            //
            GameLayerLayout.Init();                
            updateLayer = new SEL_SCHEDULE(this.LayerUpdate);

            BG = new BackGround(0);
            addChild(BG,3);
            BG.position = new CCPoint(size.width / 2, size.height / 2);



            addChild(ResDef.g_WolfBatchNode, 5);
            addChild(ResDef.g_BulletBatchNode, 7);
            addChild(ResDef.g_BalloonBatchNode, 6);
            addChild(ResDef.g_BunnyBatchNode, 5);
            addChild(ResDef.g_ShieldBatchNode, 6);
            addChild(ResDef.g_UFOBatchNode, 8);
            //
            WeatherSprite = new CCSprite();
            WeatherSprite.initWithFile(ResDef.g_BlankTextureFile);
            WeatherSprite.position = new CCPoint(size.width / 2, size.height / 2);
            CurrentWeather = ResDef.WeatherType.WT_Sun;
            addChild(WeatherSprite, 100);//最前面

            //sound
            SimpleAudioEngine.sharedEngine().playBackgroundMusic(ResDef.g_BackgroundMusic,true);

            //
            MainPlayer = new BunnyPlayer();
            MainPlayer.Weapon.Enable = true;
            //
            WolfHouse = new WolfEmitter();
            WolfHouse.position = GameLayerLayout.WolfBornPoint;
            WolfHouse.Direction = new Vector2(0, -1);
            WolfHouse.DelayTime = 1.3f;
            WolfHouse.Strength = 2;
            //          
            WolfHouse.Enable = true;

            BossHouse = new BossEmitter();
            BossHouse.position = GameLayerLayout.WolfBornPoint;
            BossHouse.Direction = new Vector2(0, -1);

            return true;
        }

        public static CCScene scene()
        {
            CCScene scene = CCScene.node();

            CCLayer layer = GameLayer.node();

            scene.addChild(layer);

            return scene;
        }

        public static new CCLayer node()
        {
            GameLayer ret = new GameLayer();
            if (ret.init())
            {
                return ret;
            }
            else
            {
                ret = null;
            }

            return ret;
        }
        //
        public override bool ccTouchBegan(CCTouch touch, CCEvent event_)
        {
            MainPlayer.TouchTimer = 0;
            return base.ccTouchBegan(touch, event_);
        }
        //
        public override void ccTouchesEnded(List<CCTouch> touches, CCEvent event_)
        {
            CCTouch touch = touches.FirstOrDefault();
            CCPoint location = touch.locationInView(touch.view());
            location = CCDirector.sharedDirector().convertToGL(location);
            if (CCRect.CCRectContainsPoint(GameLayerLayout.rightTouchRect, location))
            {
                MainPlayer.Shoot();
            }
            else if (CCRect.CCRectContainsPoint(GameLayerLayout.leftTouchRect, location))
            {
                MainPlayer.PlayMoveToPos(new CCPoint(GameLayerLayout.PlayerPole,location.y));
            }
            //
            
        }
        public override void onEnter()
        {
            MainPlayer.PlayReleaxAnimation();
            MainPlayer.Weapon.Enable = true;
            WolfHouse.Enable = true; 
            this.schedule(updateLayer, 0.03f);
            base.onEnter();
        }
        public override void onExit()
        {
            MainPlayer.Weapon.Enable = false;
            WolfHouse.Enable = false;
            this.unschedule(updateLayer);
            base.onExit();
        }
        //
        public void LayerUpdate(float dt)
        {
            if (GameStatusManager.instance.GetPlayStatus() == GamePlayStatus.GAMEPLAYSTATUS_NORMAL)
            {
                WolfHouse.Emit(dt);
            }

            if (GameStatusManager.instance.GetPlayStatus() == GamePlayStatus.GAMEPLAYSTATUS_BOSS && m_bBossAppear == true)
            {
                BossHouse.Emit(dt);
                m_bBossAppear = false;
            }            
            MainPlayer.update(dt);
            //记录主角位置
            PlayerPosition = MainPlayer.position;
            //
            if (BunnyPlayer.KillingNum == 1)
            {
                BG.PlayChangeBG(1);
                MakeRain();
            }
            if (BunnyPlayer.KillingNum == 3)
            {
                MainPlayer.Weapon.EType = BulletEmitter.EmitType.ET_Treble;
            }
            //
            string scoreLabel = "Score:" + BunnyPlayer.CurrentScore.ToString().PadLeft(8, '0');
            pGameLabel.setString(scoreLabel);
            //
            DynamicMgr.Instance().Update(dt);

            DynamicMgr.Instance().EnemyCollideMainPlayer(MainPlayer);
        }
        //
        public void MakeRain()
        {
            if (CurrentWeather == ResDef.WeatherType.WT_Rain) return;
            //
            CurrentWeather = ResDef.WeatherType.WT_Rain;

            List<CCSpriteFrame> temp = ResDef.g_WeatherDict[CurrentWeather];
            CCAnimation ani = CCAnimation.animationWithFrames(temp, 0.2f);
            CCAnimate action = CCAnimate.actionWithDuration(1.0f, ani, true);
            WeatherSprite.runAction(CCRepeatForever.actionWithAction(action));
        }
        public void MainSunshine()
        {
            CurrentWeather = ResDef.WeatherType.WT_Sun;
            WeatherSprite.stopAllActions();
        }
    }
}
